﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RestSharp;
using heyirunde.wx;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace heyirunde.wx
{
    /// <summary>
    /// getshorturl  长链接转短连接
    /// </summary>
    public class getshorturl : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string url = context.Request.Form["url"];

            JsonHelper jsonHelper = new JsonHelper();
            RestClient client = new RestClient("https://api.weixin.qq.com/cgi-bin/shorturl?access_token="+ wxconfig.getToken());

            RestRequest request = new RestRequest();
            request.Method = Method.POST;
          //  request.AddParameter("access_token", wxconfig.getToken());
           // request.AddParameter("action","long2short");
           // request.AddParameter("long_url",url);
            request.AddJsonBody(new {
                //access_token = wxconfig.getToken(),
                action = "long2short",
                long_url=url
            });          
            IRestResponse res = client.Execute(request);
            JObject obj = (JObject)JsonConvert.DeserializeObject(res.Content);
            context.Response.Write(jsonHelper.ObjToJson(new { status = 200, items = obj }));
            return;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}